package com.twb.auth.service.impl;

import com.twb.auth.entity.twbOrganization.vo.SaveTwbOrganizationVo;
import com.twb.auth.entity.twbTenantInfo.TwbTenantInfo;
import com.twb.auth.entity.twbTenantInfo.dto.SearchTwbTenantInfoDto;
import com.twb.auth.entity.twbTenantInfo.vo.SaveTwbTenantInfoVo;
import com.twb.auth.entity.twbTenantInfo.vo.SearchTwbTenantInfoVo;
import com.twb.auth.entity.twbTenantInfo.vo.UpdateTwbTenantInfoByIdVo;
import com.twb.auth.mapper.TwbTenantInfoMapper;
import com.twb.auth.service.ITwbOrganizationService;
import com.twb.auth.service.ITwbTenantInfoService;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.ExcelUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.system.util.ExcelDataDicHandlerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @ClassName TwbTenantInfoService
 * @Description 公司信息service
 * @author DSY
 * @date 2019-12-01 22:31:02
 */
@Service("twbTenantInfoService")
public class TwbTenantInfoService extends BaseService<TwbTenantInfoMapper,TwbTenantInfo> implements ITwbTenantInfoService {

	@Autowired
	TwbTenantInfoMapper twbTenantInfoMapper;

	@Autowired
	ITwbOrganizationService twbOrganizationService;

	@Override
	public TwbPageInfo<SearchTwbTenantInfoDto> searchTwbTenantInfo(SearchTwbTenantInfoVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchTwbTenantInfoDto> list = twbTenantInfoMapper.searchTwbTenantInfo(vo);
		return TwbPageUtil.of(list);
	}

	@Override
	public List<SearchTwbTenantInfoDto> searchTwbTenantInfoList(SearchTwbTenantInfoVo vo) {
		TwbPageUtil.orderBy(vo.getSorts());
		List<SearchTwbTenantInfoDto> list = twbTenantInfoMapper.searchTwbTenantInfo(vo);
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveTwbTenantInfo(SaveTwbTenantInfoVo vo) {
		TwbTenantInfo record = new TwbTenantInfo();
		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(vo.getCode());
		this.setCreateInfo(record);

		record.setUpdateDate(LocalDateTime.now());
		// 保存成功后，同步新建组织机构
		SaveTwbOrganizationVo organizationVo = new SaveTwbOrganizationVo();
		organizationVo.setTenantId(record.getId());
		organizationVo.setCode(vo.getCode());
		organizationVo.setName(vo.getName());
		organizationVo.setOrgType("0");
		String orgId = twbOrganizationService.saveTwbOrganization(organizationVo);
		boolean result = false;
		if(orgId != null) {
			record.setOrgId(orgId);
			result = this.save(record);
		}
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateTwbTenantInfoById(UpdateTwbTenantInfoByIdVo vo) {
		TwbTenantInfo tenantInfo = twbTenantInfoMapper.selectById(vo.getId());
		if (null == tenantInfo) {
			throw new ServiceException("数据不存在");
		}
		BeanUtils.copyProperties(vo, tenantInfo);
		this.setUpdateInfo(tenantInfo);
		return this.updateById(tenantInfo);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteTwbTenantInfoById(String id) {
		return this.removeById(id);
	}

	@Override
	public void exportExcel(SearchTwbTenantInfoVo searchVo, HttpServletResponse response) {
		TwbPageInfo<SearchTwbTenantInfoDto> pageInfo = this.searchTwbTenantInfo(searchVo);
		ExcelDataDicHandlerImpl excelDataDicHandlerImpl = new ExcelDataDicHandlerImpl();
		ExcelUtils.exportExcel(searchVo,"租户信息", pageInfo.getList(), SearchTwbTenantInfoDto.class, response, excelDataDicHandlerImpl);
	}

	@Override
	public int importExcel(Map<String, String> map) {
		System.out.println(map.toString());
		return 0;
	}

	@Override
	public boolean deleteTwbTenantInfoByIds(@NotBlank String ids) {

		return this.removeByIds(Arrays.asList(ids.split(",")));
	}

	@Override
	public int logicBatchDeleteTwbTenantInfoByIds(String ids) {
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, ids.split(","));
		return twbTenantInfoMapper.logicBatchDeleteByIds(list);
	}
}
